package application.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import library.Controller;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import application.models.Modwineusers;



/**
 * Servlet implementation class welcome
 */
public class administrator extends Controller {
	private static final long serialVersionUID = 1L;

	public void login(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		if(request.getSession().getAttribute("user")!= null) {
			response.sendRedirect(name+"?op=home");
		} else {
			out.flush();
			include(request,response,"/layout/"+name+"/login.jsp");
		}
	}
	
	public void home(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		try {
			String name = this.getClass().getSimpleName();
			if(request.getSession().getAttribute("user")!= null) {
				out.flush();
				include(request,response,"/layout/"+name+"/home.jsp");
			} else {
				response.sendRedirect(name+"?op=login");
			}
		}catch(Exception e){
			PrintWriter x = response.getWriter();
			x.write(e.toString()+":"+e.getLocalizedMessage());			
		}
	}
	
	@SuppressWarnings("unchecked")
	public void autenticate(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		try {
			//Fetch of data send into request
			String username=request.getParameter("user"), passwd=request.getParameter("passwd");
			String name = this.getClass().getSimpleName();
			
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			
			//Fetch user
			Query sql = session.createQuery("from Modwineusers where user_id =:user");
			List<Modwineusers> users = (List<Modwineusers>) sql.setString("user", username).list();
			if(users.isEmpty()) {
				response.sendRedirect(name+"?op=login");
			} else {
				Modwineusers user =	users.get(0);
				if(user.getPassword().equals(passwd) && user.isAdmin()) {
					request.getSession().setAttribute("user", user);
					response.sendRedirect(name+"?op=home");
				} else {
					response.sendRedirect(name+"?op=login");
				}
			}
			
			session.getTransaction().commit();
			session.close();
		} catch(Throwable e){
			PrintWriter x = response.getWriter();
			x.write(e.toString()+":"+e.getLocalizedMessage());
		}

	}
	
	public void logout(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		try {
			String name = this.getClass().getSimpleName();
			if(request.getSession().getAttribute("user")!= null)
				request.getSession().removeAttribute("user");

			response.sendRedirect(name+"?op=login");
			
		} catch(Exception e) {
			PrintWriter x = response.getWriter();
			x.write(e.toString()+":"+e.getLocalizedMessage());
		}
	}
}
